/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.demension;

import cn.com.jsepc.rep.comm.Common;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.report.FillDataDAO;
import cn.com.jsepc.rep.vo.DementionVO;
import cn.com.jsepc.rep.vo.RepDataVO;
import cn.com.jsepc.rep.vo.SelectGridVO;
import cn.com.jsepc.rep.vo.TreeVO;
import com.jarp.db.DAO;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author 赵远
 */
public class DementionDAO {

    public String updata_rep_Demension(String repid, String objInfo) {       //更新指标维度信息表
        DAO dao = new DAO();
        String str="";
        if(objInfo.equals("{}")){    //传进来空值，取消该指标关联的维度
            String delete_sql="delete from tb_rep_demension where repid="+repid;
            String query_sql="select * from tb_rep_demension where repid="+repid;
            FillDataDAO filldao = new FillDataDAO();
            RepDataVO repVo = filldao.getRepDemension(repid);
            String tablename = ConstUtil.FACTPRE + repVo.getRepcode();
            String Drop_sql = "drop table " + tablename;
            String rep_sql = "update tb_report set reffact=NULL,REPVALUECOL='' where repid=" + repid;
            try {
            	dao.execCommand(ConstUtil.DBNAME, rep_sql);
				dao.execCommand(ConstUtil.DBNAME, Drop_sql);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            try {
                Vector v=dao.getDataSet(ConstUtil.DBNAME, query_sql);
                if(v==null){
                    str="ok";
                }
                else{
                   int irows= dao.execCommand(ConstUtil.DBNAME, delete_sql);
                   if(irows>=1){
                       str="ok";
                   }
                }
            } catch (ServletException ex) {
                Logger.getLogger(DementionDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
        JSONObject jsonObject = JSONObject.fromString(objInfo);
        JSONArray jsonArr = jsonObject.getJSONArray("listData");
        
        ArrayList list=new  ArrayList();
        String sql_DEL = "";
        try {

            boolean b = true;
           Common com=new Common();
            sql_DEL = "DELETE FROM tb_rep_demension WHERE REPID=" + repid;
            dao.execCommand(ConstUtil.DBNAME, sql_DEL);
            for (int i2 = 0; i2 < jsonArr.length(); i2++) {
                JSONObject json = jsonArr.getJSONObject(i2);
              String  AVAILSNAME = json.getString("availsname");
              String  DIMORDER = json.getString("dimorder");
              String  DIMID=json.getString("demid");
              String  factdimcol = this.getFacdimcol(DIMID);
                if(DIMORDER.equals("")){
                    DIMORDER="1";
                }

               String sql_INSERT2 = "INSERT INTO tb_rep_demension(ID,REPID,DIMID,HIRECDESC,FACTDIMCOL,AVAILSNAME,DIMORDER) VALUES("
                        + com.getSeqStr("SEQ_REP_DEM_HIREC")+","
                        + repid + ","
                        + DIMID + ",'"
                        + "  " + "','"
                        + factdimcol + "','"
                        + AVAILSNAME + "',"
                        + DIMORDER
                        + ")";
                list.add(sql_INSERT2);
            }
            if (list.size()>0)
            	b=dao.execCommand(ConstUtil.DBNAME, list);
            if (b == true) {
                str= "ok";
            } else {
                str= "wrong";
            }
        } catch (ServletException ex) {
            Logger.getLogger(DementionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        return str;
    }

public String getSelect_Grid(String repid){
         String res="";
         String sql="select "
                 + "a.demid,"
                 + "a.demname,"
                 +"a.dimtype,"
                 + "b.availsname,"
                 + "b.factdimcol,"
                 + "b.hirecdesc,"
                 + "b.repid,"
                 + "b.DIMORDER as dimorder from tb_demension"
                 + " a left join "
                 + "tb_rep_demension b "
                 + "on b.repid="+repid
                 + " and a.demid=b.dimid where a.isvalid=1";
        DAO dao=new DAO();
        try {
            List<SelectGridVO> lstResults = new ArrayList<SelectGridVO>();
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    SelectGridVO gridVO = new SelectGridVO();
                    gridVO.setDemid((String) hashRows.get("DEMID"));
                    gridVO.setDemname((String) hashRows.get("DEMNAME"));
                    gridVO.setAvailsname((String)hashRows.get("AVAILSNAME"));
                    gridVO.setDimtype((String)hashRows.get("DIMTYPE"));
                    gridVO.setDimorder(((String)hashRows.get("DIMORDER")).trim());
                    gridVO.setFactdimCol((String)hashRows.get("FACTDIMCOL"));
                    gridVO.setIsselect((String)hashRows.get("REPID"));
                    gridVO.setRepid(repid);
                    lstResults.add(gridVO);
                }
                JSONArray json = new JSONArray();
                json = JSONArray.fromObject(lstResults);                
                res = json.toString();
            }
        } catch (ServletException ex) {
        }
         return res;
     }

public String getFacdimcol(String dimid){
    DAO dao=new DAO();
    String str="";
    String sql="SELECT * FROM TB_DEMENSION WHERE DEMID="+dimid;
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v!=null){
                Hashtable hashRows = (Hashtable) v.get(0);
                String facdimcol=(String)hashRows.get("FACDIMCOL");
                str=facdimcol;
            }
        } catch (ServletException ex) {
            Logger.getLogger(DementionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return str;
}
}
